package com.itheima.spider.version2;

import com.google.gson.Gson;
import com.itheima.spider.dao.NewsDao;
import com.itheima.spider.pojo.News;
import com.itheima.spider.utils.FinalRedisKeyUtils;
import com.itheima.spider.utils.JedisUtils;
import redis.clients.jedis.Jedis;

import java.sql.SQLException;
import java.util.List;

public class PublicDaoNode {
    //从redis 中获取json数据,将 json数据转为news对象对象,去重判断,保存数据,将保存后的docurl保存到redis中list中
    //循环获取
    private static NewsDao newsDao = new NewsDao();

    public static void main(String[] args) throws SQLException {
        while (true) {
            Jedis jedis = JedisUtils.getJedis();
            List<String> list = jedis.brpop(20, FinalRedisKeyUtils.NEWS_JSON_REDIS_LIST);
            jedis.close();
            if (list == null || list.size() == 0) {
                break;
            }

            String newsJson = list.get(1);
            Gson gson = new Gson();
            News news = gson.fromJson(newsJson, News.class);
            //去重判断
            jedis = JedisUtils.getJedis();
            Boolean flag = jedis.sismember(FinalRedisKeyUtils.NO_REPEAT_REDIS_SET, news.getDocurl());
            jedis.close();
            if (flag) {
                continue;
            }


            //4)保存数据
            newsDao.saveNews(news);

            //5)将保存后的docurl 添加到redis中
            jedis = JedisUtils.getJedis();
            jedis.sadd(FinalRedisKeyUtils.NO_REPEAT_REDIS_SET, news.getDocurl());

            jedis.close();


        }
    }
}
